$(document).ready(function(){
 $("#atype").change(function(){
	$("#atype option").each(function(i,o){
		if($(this).attr("selected")) {
			$(".user").hide();
			$(".user").eq(i).show();
		}});
	});
$("#atype").change();

$('#select_button').click(function() {
	var user_id = $('select.user_id').val();
	var group_id = $('select.group_id').val();
	if (user_id == 0 && group_id == 0) {
		alert('请选择用户!');
	} else {
		if (user_id != 0) {
			var id = user_id;
		} else {
			var id = group_id;
		}
		window.location = '/user/edit/' + id;
	}
});


$('#edit_user_form').submit(function() {
	var username = $('#username').fieldValue();
	var password = $('#password').fieldValue();
	var repassword = $('#repassword').fieldValue();
	var atype = $('#create_user_form input[name=atype]').fieldValue();
	var email = $('#email').fieldValue();
	var mobile = $('#mobile').fieldValue();
	var remark = $('#remark').fieldValue();


/*
	var username_flag = true;
	$('#username').focus(function() {
		$('#username_msg').empty();
	});
	if(username.toString().length == 0) {
		$('#username_msg').html('<span style="color:red;">用户名不能为空</span>');
		username_flag = false;
	}
	
	var password_flag = true;
	$('#password').focus(function() {
		$('#password_msg').empty();
	});
	if(password.toString().length == 0) {
		$('#password_msg').html('<span style="color:red;">密码不能为空</span>');
		password_flag = false;
	}
*/
	var repassword_flag = true;
	$('#repassword').focus(function() {
		$('#repassword_msg').empty();
	});
	if (password.toString() != repassword.toString()) {
		$('#repassword_msg').html('<span style="color:red;">密码不一致</span>');
		repassword_flag = false;
	}


	var mobile_flag = true;
	$('#mobile').focus(function() {
		$('#mobile_msg').empty();
	});
	var re_mobile = /^[0-9]{11}$/;
	if (!re_mobile.test(mobile.toString()) && mobile.toString().length > 0) {
		$('#mobile_msg').html('<span style="color:red;">手机格式有误</span>');
		repassword_flag = false;
	}

	var email_flag = true;
	$('#email').focus(function() {
		$('#email_msg').empty();
	});
	var re_email=/^[-.0-9a-zA-Z]+@([-0-9a-zA-Z]+\.)+[-0-9a-zA-Z]+$/; 
	if (!re_email.test(email.toString()) && email.toString().length > 0) {
		$('#email_msg').html('<span style="color:red;">Email格式有误</span>');
		repassword_flag = false;
	}

	//if (username_flag && password_flag && repassword_flag && mobile_flag  && email_flag) {
	if (repassword_flag && mobile_flag  && email_flag) {
		$.post('/user/edit_do', $('#edit_user_form').serialize(), function(data){
					if (data.r == 'succ') {
						alert('操作成功');
					} else {
						alert('操作失败' +  "\n>" +  data.msg.join("\n>"));
					}
				}, 'json');
	}
	return false;

});




});
